package server.map;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import server.utility.Id;
import server.utility.Point2d;


public class Gate implements Serializable{

	private static final long serialVersionUID = 1L;
	private Id id, destinationMap;
	private Point2d top, bottom, destination;
	
	public Gate(Id destMap, Point2d t, Point2d b, Point2d dest){
		id=Id.nextId();
		destinationMap=destMap;
		destination=dest;
		top=t;
		bottom=b;
	}
	
	public boolean isInside(Point2d p){
		boolean inX=p.getX()>=top.getX() && p.getX()<=bottom.getX();
		boolean inY=p.getY()>=top.getY() && p.getY()<=bottom.getY();
		
		return inX && inY;
	}

	public Id getDestinationMap() {
		return destinationMap;
	}
	
	public Point2d getDestination(){
		return destination;
	}
	
	private void readObject(ObjectInputStream in) throws IOException,
	ClassNotFoundException {
		int ver=in.readInt();
		
		switch(ver){
		
			case 1:{
				top = (Point2d)in.readObject();
				bottom = (Point2d)in.readObject();
				destinationMap = (Id)in.readObject();
				id = (Id)in.readObject();
				destination=(Point2d) in.readObject();
			}
		}
	}
	
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.writeInt(1); //version
		
		out.writeObject(top);
		out.writeObject(bottom);
		out.writeObject(destinationMap);
		out.writeObject(id);
		out.writeObject(destination);
	}
}
